<template>
  <!-- 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getRoleList } from '@/api/settings'
import { getUserDetail } from '@/api/user'
import { assignRoles } from '@/api/employees'

export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isShowDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      checkList: [],
      roleList: []
    }
  },
  created() {
    this.loadRoleList()
    this.loadUserDetail()
  },
  methods: {
    async loadRoleList() {
      const res = await getRoleList()
      console.log('roleList', res)
      this.roleList = res.data.rows
    },
    closeDialog() {
      this.$emit('update:isShowDialog', false)
    },
    async loadUserDetail() {
      const res = await getUserDetail(this.id)
      console.log(111, res)
      this.roleIds = res.data.roleIds
    },
    // 更新用户信息
    async submit() {
      try {
        await assignRoles({ roleIds: this.roleIds, id: this.id })
        this.$message.success('分配成功')
        this.$emit('update:isShowDialog', false)
      } catch (error) {
        this.$message.error('分配成功')
        console.log(error)
      }
    }
  }
}
</script>
